<?php


namespace App\Models;


class RoleScope
{
    // 超级管理员
    const ROLE_SUPER = 10;
    // 管理员
    const ROLE_ADMIN = 20;

    // 课程管理员
    const ROLE_COURSE_LEADER = 100;
    // 课程访客
    const ROLE_COURSE_VISITOR = 110;

    // 客服管理员
    const ROLE_CUSTOM_SERVICE_LEADER = 200;

    const ROLES_NAME = [
        self::ROLE_SUPER => '超级管理员',
        self::ROLE_ADMIN => '管理员',
        self::ROLE_COURSE_LEADER => '课程管理员',
        self::ROLE_COURSE_VISITOR => '课程访客',
        self::ROLE_CUSTOM_SERVICE_LEADER => '客服管理员'

    ];


    public static function getAllRoles()
    {
        $roles = [];
        foreach (static::ROLES_NAME as $id =>  $name) {
            $roles[] = [
                'id' => $id,
                'name' => $name
            ];
        }
        return $roles;
    }

    public static function getRoleName($roleId)
    {
        return static::ROLES_NAME[$roleId];
    }

    public static function checkValidRoleId($roleId)
    {
        $validRoleIds = [
            static::ROLE_SUPER,
            static::ROLE_ADMIN,
            static::ROLE_COURSE_LEADER,
            static::ROLE_COURSE_VISITOR,
            static::ROLE_CUSTOM_SERVICE_LEADER,
        ];
        return in_array($roleId, $validRoleIds);
    }

    public static function roleToScopes($roleId)
    {
        switch ($roleId) {
            // 超级管理员
            case static::ROLE_SUPER:
                $scopes = [
                    'admin',
                    'admin-course-leader',
                    'admin-course-visitor',
                    'admin-custom-service-leader'
                ];
                break;
            // 管理员
            case static::ROLE_ADMIN:
                $scopes = [
                    'admin',
                    'admin-course-leader',
                    'admin-course-visitor',
                    'admin-custom-service-leader'
                ];
                break;
            // 课程管理员
            case static::ROLE_COURSE_LEADER:
                $scopes = ['admin-course-visitor', 'admin-course-leader'];
                break;
            case static::ROLE_COURSE_VISITOR:
                $scopes = ['admin-course-visitor'];
                break;
            // 客户服务
            case static::ROLE_CUSTOM_SERVICE_LEADER:
                $scopes = ['admin-custom-service-leader'];
                break;
            // 其他
            default:
                $scopes = [];
        }
        return $scopes;
    }

    public static function getValidScopes()
    {
        return static::roleToScopes(static::ROLE_ADMIN);
    }
}